layui.config({
    version: '318',   // 更新组件缓存，设为true不缓存，也可以设一个固定值
    base: 'assets/module/'
}).extend({
    steps: 'steps/steps',
    notice: 'notice/notice',
    cascader: 'cascader/cascader',
    dropdown: 'dropdown/dropdown',
    fileChoose: 'fileChoose/fileChoose',
    Split: 'Split/Split',
    Cropper: 'Cropper/Cropper',
    tagsInput: 'tagsInput/tagsInput',
    citypicker: 'city-picker/city-picker',
    introJs: 'introJs/introJs',
    zTree: 'zTree/zTree'
}).use(['layer', 'setter', 'index', 'admin', 'notice', 'form'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var setter = layui.setter;
    var index = layui.index;
    var admin = layui.admin;
    var notice = layui.notice;
    var form = layui.form;

    var iphost = "";
    var token =  "";

    function configServer() {
        admin.open({
            type: 1,
            title: '配置服务器',
            content: $('#serverConfigDialog').html(),
            area: ["60%", "60%"],
            closeBtn: 0,
            success: function (layero, dIndex) {
                form.val('serverConfigForm', { iphost, token });
                form.on('submit(serverConfigSubmit)', function (data) {
                    var param = JSON.stringify(data.field)
                    formium.sendHostWindowRequestAsync("SubmitServerConfig", param).then((data) => {
                        window.location.reload()
                    })
                })
            }
        })
    }

    formium.sendHostWindowRequestAsync("GetServerConfig").then((json) => {
        var d = JSON.parse(json)

        if (d.code != 200) {
            layer.msg("配置/连接 错误：" + d.msg);
        }

        iphost = d.data ? d.data.iphost : "";
        token = d.data ? d.data.token : "";

        if (iphost == null || iphost == '' || token == null || token == '') {
            admin.removeLoading();
            configServer()
            return;
        } else {
            initPage();
        }
    })

    function initPage() {
        /* 加载侧边栏 */
        admin.req('/json/menus.json', function (res) {
            if (0 === res.code) {
                index.regRouter(res.data, function (data) {
                    data.name = data.title;
                    data.url = data.path;
                    data.iframe = data.component;
                    data.show = !data.hide;
                    data.subMenus = data.children;
                    return data;
                });  // 注册路由
                index.renderSide(res.data);  // 渲染侧边栏
                // 加载主页
                index.loadHome({
                    url: '#/example/file',
                    name: '<i class="layui-icon layui-icon-home"></i>'
                });
            } else {
                layer.msg('获取菜单列表失败', { icon: 2, anim: 6 });
            }
        });
    }


});